package com.zd.kids.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zaaach.citypicker.CityPickerDialogFragment;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zd.kids.DataBean.CountryDataBean;
import com.zd.kids.R;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectCityActivity extends BaseActivity {


    public static final int SELECT_RESULT_CODE = 1001;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_select_city;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        setTheme(R.style.DefaultCityPickerTheme);

        initTitle(getString(R.string.select_country));

        getCountyDatas();
    }

    private void initFragment(List<City> countryDataBeans) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        CityPickerDialogFragment cartFragment = CityPickerDialogFragment.newInstance(false, countryDataBeans);

        cartFragment.setOnPickListener(new OnPickListener() {
            @Override
            public void onPick(int position, City data) {

                Intent intent = new Intent();
                intent.putExtra("cityCode", data.getCode());
                intent.putExtra("cityId", data.getProvince());
                setResult(SELECT_RESULT_CODE, intent);
                finish();
            }

            @Override
            public void onLocate() {

            }

            @Override
            public void onCancel() {

            }
        });
        fragmentTransaction.add(R.id.fl_content, cartFragment);
        fragmentTransaction
                .show(cartFragment)
                .commit();
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }

    private void getCountyDatas() {


        showLoading();
        HttpMethod.getIntence().getCountrys(new CustomObserver<List<CountryDataBean>>(this) {
            @Override
            public void onSuccess(List<CountryDataBean> countryDataBeans) {
                disLoading();
//                countryDataBeans
                List<City> cityList = new ArrayList<>();

                for (CountryDataBean countryDataBean : countryDataBeans) {
                    City city = new City(countryDataBean.getCountryname() + countryDataBean.getCountrycode(), countryDataBean.getCountryid() + "", countryDataBean.getCountryNamePinyin(), countryDataBean.getCountrycode());
                    cityList.add(city);
                }

                initFragment(cityList);

            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }

}
